<?php
require_once dirname(__FILE__) . '/../common/Common.php';
require_once dirname(__FILE__) . '/../dao/dao_live/dao_live.class.php';
require_once dirname(__FILE__) . '/../conf/OutDefine.php';
require_once dirname(__FILE__) . '/../common/TLSSig.php';

class GetUserSign extends AbstractInterface//获取用户UserSin
{
    public function initialize()
    {
        return true;
    }

    public function verifyInput(&$args)//获取参数
    {
        $req = $args['interface']['para'];

        $rules = array(
		//'参数名'=>array('type'=>'参数字段类型');
            'userid' => array('type' => 'string'),
            'password' => array('type' => 'string'),
            //'groupid'=>array('type' => 'string'),
        	//'title' => array('type' => 'string'),
        );
        return $this->_verifyInput($args, $rules);
    }

    public function process()
    {
        interface_log(INFO, EC_OK,"GetUserSign args=" . var_export($this->_args, true));
        $config = getConf('ROUTE.DB');
        //验证用户账号密码
        $dao_live = new dao_live($config['HOST'], $config['PORT'], $config['USER'], $config['PASSWD'], $config['DBNAME']);
        $error_message = "";
    	$ret = $dao_live->checkUserPwd($this->_args['userid'],$this->_args['password'] );
    	if($ret != 1)//不等于1就是不存在用户
    	{
    		$this->_retValue ='User`s account or password error';
    		$error_message="db error:no permission";
    		$this->_retMsg = 'GetUserSign::process() fail '.genErrMsg($this->_retValue);
    		return false;
    	}
        try{
            $api = new TLSSigAPI();
            $api->SetAppid(APP_ID);
            $private = file_get_contents('/home/private_key');
            $api->SetPrivateKey($private);
//            $public = file_get_contents(dirname(__FILE__).DIRECTORY_SEPARATOR.'public.pem');
//            $api->SetPublicKey($public);
            $sig = $api->genSig($this->_args['userid']);
//            $result = $api->verifySig($sig, 'user1', $init_time, $expire_time, $error_msg);
//            var_dump($result);
//            var_dump($init_time);
//            var_dump($expire_time);
//            var_dump($error_msg);
//            interface_log(INFO,EC_OK,'This sign is:'.$sig);
//            $result = $api->verifySig($sig, $this->_args['userid'], $init_time, $expire_time, $error_msg);
//            var_dump($result);
//            var_dump($init_time);
//            var_dump($expire_time);
//            var_dump($error_msg);
        }catch(Exception $e){
            echo $e->getMessage();
        }

        $this->_retValue = EC_OK;//返回数据的状态码
        $this->_data=array("UserSign" => $sig);//返回数据的数据数组
        interface_log(INFO, EC_OK, 'GetUserSign::process() succeed');
        return true;
    }
}

?>
